package cn.iocoder.yudao.module.picturebook.controller.admin.membership;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.membership.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.membership.MembershipDO;
import cn.iocoder.yudao.module.picturebook.service.membership.MembershipService;

@Tag(name = "绘本系统 - 会员信息")
@RestController
@RequestMapping("/picturebook/membership")
@Validated
public class MembershipController {

    @Resource
    private MembershipService membershipService;

    @PostMapping("/create")
    @Operation(summary = "创建会员信息")
    @PreAuthorize("@ss.hasPermission('picturebook:membership:create')")
    public CommonResult<Long> createMembership(@Valid @RequestBody MembershipSaveReqVO createReqVO) {
        return success(membershipService.createMembership(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员信息")
    @PreAuthorize("@ss.hasPermission('picturebook:membership:update')")
    public CommonResult<Boolean> updateMembership(@Valid @RequestBody MembershipSaveReqVO updateReqVO) {
        membershipService.updateMembership(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:membership:delete')")
    public CommonResult<Boolean> deleteMembership(@RequestParam("id") Long id) {
        membershipService.deleteMembership(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除会员信息")
                @PreAuthorize("@ss.hasPermission('picturebook:membership:delete')")
    public CommonResult<Boolean> deleteMembershipList(@RequestParam("ids") List<Long> ids) {
        membershipService.deleteMembershipListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:membership:query')")
    public CommonResult<MembershipRespVO> getMembership(@RequestParam("id") Long id) {
        MembershipDO membership = membershipService.getMembership(id);
        return success(BeanUtils.toBean(membership, MembershipRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员信息分页")
    @PreAuthorize("@ss.hasPermission('picturebook:membership:query')")
    public CommonResult<PageResult<MembershipRespVO>> getMembershipPage(@Valid MembershipPageReqVO pageReqVO) {
        PageResult<MembershipDO> pageResult = membershipService.getMembershipPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MembershipRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员信息 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:membership:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMembershipExcel(@Valid MembershipPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MembershipDO> list = membershipService.getMembershipPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员信息.xls", "数据", MembershipRespVO.class,
                        BeanUtils.toBean(list, MembershipRespVO.class));
    }


}